﻿using System.Collections.Generic;
using Newtonsoft.Json;
namespace CRMSystemV2.Common
{
    public static class JsonHelper
    {
        /// <summary>
        /// 反序列化对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="jsonData"></param>
        /// <returns></returns>
        public static T DeserializeObject<T>(string jsonData)where T:class
        {
            return (T)JsonConvert.DeserializeObject(jsonData, typeof(T));
        }
        /// <summary>
        /// 反序列化对象集合
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="jsonData"></param>
        /// <returns></returns>
        public static List<T> DeserializeObjectList<T>(string jsonData) where T : class
        {
            return (List<T>)JsonConvert.DeserializeObject<List<T>>(jsonData);
        }
        /// <summary>
        /// 序列化对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string SerializeObject<T>(T obj) where T : class
        {
            return JsonConvert.SerializeObject(obj);
        }
        /// <summary>
        /// 序列化对象集合
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string SerializeObject<T>(List<T> obj) where T : class
        {
            return JsonConvert.SerializeObject(obj);
        }
    }
}
